/**
 * @file: IHM.java
 * @author: Bon William <william.bon2@gmail.com>
 * @author: Ernewein Audrey <audrey.ernewein@gmail.com>
 * @brief:
 * This file contains functions to build the IHM and adding actions to be executed
 * when pressing the buttons.
 */
package generator;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

@SuppressWarnings("serial")
public class IHM extends JFrame {
	JButton generateButton = new JButton("Generate a file");
	JButton generateAll = new JButton("Generate all files");
	
	JCheckBox selfComplementary = new JCheckBox("Self complementary");
	JCheckBox maxCheckBox = new JCheckBox("Maximal");
	
	String workingDir =new String();
	JLabel workingDirLabel = new JLabel("No working directory set");
	JButton setWorkingDir = new JButton("Set working directory");
	
	JPanel workingDirPanel = new JPanel();
	JPanel buttonPanel = new JPanel();
	JPanel argPanel = new JPanel();
	JPanel checkboxPanel = new JPanel();
	JPanel spinPanel = new JPanel();
	
	GridLayout grid = new GridLayout(2,1);
	
	JLabel spinLabel = new JLabel("Code's length :");
	
	JSpinner spinner = new JSpinner();
	
	/**
	 * IHM main constructor:
	 * builds the IHM elements and position it insinde the window
	 */
	public IHM() {
		
		/* Creating spinner for choosing code length */
		SpinnerModel model = new SpinnerNumberModel(1,1,6,1);
		spinner.setModel(model);
		
		/* Adding action listener to buttons */
		setWorkingDir.addActionListener(new boutonWorkingDirListener());
		generateButton.addActionListener(new boutonGenerateListener());
		generateAll.addActionListener(new boutonGenerateListener());
		
		/* Panel creation and positioning */
		spinPanel.add(spinLabel,BorderLayout.WEST);
		spinPanel.add(spinner,BorderLayout.EAST);
		
		workingDirPanel.add(setWorkingDir,BorderLayout.SOUTH);
		workingDirPanel.add(workingDirLabel,BorderLayout.NORTH);
		
		checkboxPanel.add(selfComplementary,BorderLayout.EAST);
		checkboxPanel.add(maxCheckBox,BorderLayout.WEST);
		
		argPanel.setLayout(grid);
		argPanel.add(spinPanel);
		argPanel.add(checkboxPanel);
				
		buttonPanel.add(generateButton,BorderLayout.EAST);
		buttonPanel.add(generateAll,BorderLayout.WEST);

		/* setting window options (name, size, and contents */
		this.setVisible(true);
		this.setTitle("Dinucleotide Circular Codes");
		this.setSize(400, 200);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.add(argPanel,BorderLayout.NORTH);
		this.add(workingDirPanel,BorderLayout.CENTER);
		this.add(buttonPanel,BorderLayout.SOUTH);
	}
	
	/* Inside class for working directory button listener */
	class boutonWorkingDirListener implements ActionListener
	{
		/**
		 * actionPerformed
		 * Callback called when pressing on "Set working directory" button
		 */
		public void actionPerformed(ActionEvent arg0)
		{
	        
		    JFileChooser chooser = new JFileChooser(); 
		    chooser.setCurrentDirectory(new java.io.File("."));
		    chooser.setDialogTitle("Select working directory");
		    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    //
		    // disable the "All files" option.
		    //
		    chooser.setAcceptAllFileFilterUsed(false);
		    
		    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 
		      workingDir=chooser.getSelectedFile().toString()+"/";
		      workingDirLabel.setText(workingDir);
		      }
		}
	}
	
	/* Inside class for buttons' listener */
	class boutonGenerateListener implements ActionListener
	{
		/**
		 * actionPerformed
		 * Callback called when pressing on a button
		 */
		public void actionPerformed(ActionEvent arg0)
		{
			if(workingDir.isEmpty())
			{
				System.out.println("No working directory are set");
				JOptionPane.showMessageDialog((Component) arg0.getSource(), "You haven't set a working directory for exporting files");
				return ;
			}
			/* Getting user's wanted options (maximal, self complementary) and
			 * length for the Dinucleotides sets */
			boolean max=maxCheckBox.isSelected();
			boolean selfC=selfComplementary.isSelected();
			int length=(Integer)spinner.getModel().getValue();
			
			ArrayList<DinucleotideSet> list = new ArrayList<DinucleotideSet>();
			
			/* User press "generate" button */
			if(arg0.getSource().equals(generateButton))
			{
				list = DinucleotideSet.generate(length,max,selfC,workingDir);
				System.out.println("Le fichier a été généré.");
				System.out.println("Il contient : " + list.size() + " codes.\n");
			}
			else // User press "generate all" button
			{
				DinucleotideSet.generateAll(workingDir);
				System.out.println("Tous vos fichiers ont été générés.\n");
			}
		}
	
	}

}
